package unibratec.projeto.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ParadaDAO extends SQLiteOpenHelper{

	private static final String DB_NAME = "projetoparada";
	private static final String TABLE_NAME = "parada";
	
	
	public ParadaDAO(Context context) {
		super(context, DB_NAME, null, 1);
	
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLE_NAME + 
				" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
				"nome TEXT NOT NULL, " +
				"descricao TEXT NOT NULL, " +
				"lng LONG, " +
				"lat LONG)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
	
	public void inserir(Parada parada) {
		Log.d("DAO", "inserir");
		ContentValues values = this.criarValues(parada);

		SQLiteDatabase db = this.getWritableDatabase();
		db.insert(TABLE_NAME, null, values);
		db.close();
		
	}

	public void atualizar(Parada parada) {
		Log.d("DAO", "atualizar");
		ContentValues values = this.criarValues(parada);
		SQLiteDatabase db = this.getWritableDatabase();
		db.update(TABLE_NAME, values, "_id=?", 
				  new String[] { String.valueOf(parada.getId_Parada()) });
		db.close();
		
	}

	public void excluir(Parada parada) {
		Log.d("DAO", "inserir");
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_NAME, "_id=" + parada.getId_Parada(), null);
		db.close();
		
	}

	public List<Parada> listar() {
		Log.d("DAO", "listar");
		List<Parada> listaParadas = new ArrayList<Parada>();
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, "nome");
		
		while (cursor.moveToNext()) {
			long id = cursor.getLong(0);
			String nome = cursor.getString(1);
			String descricao = cursor.getString(2);

			listaParadas.add(new Parada(id, nome, descricao, 0, 0));
		
		}
		
		db.close();
		return listaParadas;
	
	}

	public List<Parada> pesquisar() {
		Log.d("DAO", "pesquisar");
		List<Parada> listaParadas = new ArrayList<Parada>();
		SQLiteDatabase db = this.getReadableDatabase();
		db.setLocale(Locale.getDefault());
		Cursor cursor = db.rawQuery("SELECT id, nome, descricao FROM TABLE_NAME", null);
		while (cursor.moveToNext()) {
			long id = cursor.getLong(0);
			String nome = cursor.getString(1);
			String descricao = cursor.getString(2);

			listaParadas.add(new Parada(id, nome, descricao, 0, 0));
			
		}
		
		db.close();
		Log.d("DAO", "pesquisar " + listaParadas.size());
		return listaParadas;

	}

	private ContentValues criarValues(Parada parada) {
		ContentValues values = new ContentValues();
		values.put("nome", parada.getNome());
		values.put("descricao", parada.getDescricao());
		values.put("lng", parada.getLng());
		values.put("lat", parada.getLat());
		return values;
	}

	public List<Parada> listaAtualizada() {
		Log.d("DAO", "listaAtualizada");
		List<Parada> listaParadas = this.pesquisar();
		for (Parada parada : listaParadas) {
			if (parada.getId_Parada() > 0) {
				listaParadas.add(parada);
			}
		}
		return listaParadas;
	}


}